<template>
  <a-modal
    title="新建"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="modalOk"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="车行名称">
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入订单类型！'}], initialValue: model.name}]" />
        </a-form-item>
        <a-form-item label="电话">
          <a-input v-decorator="['tel', {rules: [{required: true, message: '请输入收订员！'}], initialValue: model.tel}]" />
        </a-form-item>
        <a-form-item label="图片">
          <a-input v-decorator="['photo', {rules: [{required: true, message: '请输入收订员！'}], initialValue: model.photo}]" />
        </a-form-item>
        <a-form-item label="备注">
          <a-input v-decorator="['summary', {rules: [{required: true, message: '请输入收订员！'}], initialValue: model.summary}]" />
        </a-form-item>
        <a-form-item label="开放时间">
          <!-- <a-time-picker :default-value="moment('12:08', 'HH:mm')" format="HH:mm" /> -->
          <a-time-picker format="HH:mm" valueFormat="HH:mm" v-decorator="['openTime', {rules: [{required: true, message: '请输入收订员！'}], initialValue: model.openTime}]" />
        </a-form-item>
        <a-form-item label="关闭时间">
          <a-time-picker format="HH:mm" valueFormat="HH:mm" v-decorator="['closeTime', {rules: [{required: true, message: '请输入收订员！'}], initialValue: model.closeTime}]" />
          <!-- <a-input v-decorator="['closeTime', {rules: [{required: true, message: '请输入收订员！'}], initialValue: model.closeTime}]" /> -->
        </a-form-item>
        <custom-baidu-map @mapSelect="mapSelect" :detailData="model" />
        <a-form-item label="地址">
          <a-input v-decorator="['place.address', {rules: [{required: true, message: '请输入地址'}], initialValue: model.place && model.place.address}]" />
        </a-form-item>
        <a-form-item label="经度">
          <a-input v-decorator="['place.coordinate.longitude', {rules: [{required: true, message: '请输入地址'}], initialValue: model.place && model.place.coordinate.longitude}]" />
        </a-form-item>
        <a-form-item label="纬度">
          <a-input v-decorator="['place.coordinate.latitude', {rules: [{required: true, message: '请输入地址'}], initialValue: model.place && model.place.coordinate.latitude}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
// 审核状态 0=待审核 1=待开票 2=待发行 3=已完成
import pick from 'lodash.pick'
import { addCardealership, updateCardealership, getCardealership } from '@/api/cardealership'
import CustomBaiduMap from '@/views/baidu-map/CustomBaiduMap'
const fields = ['id']

export default {
  name: 'Detail',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      loading: false,
      form: this.$form.createForm(this),
      detailData: {}
    }
  },
  components: {
    CustomBaiduMap
  },
  created () {
    this.loading = true
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    const _self = this
    this.$watch('model', () => {
      this.form = this.$form.createForm(this)
      if (this.model && this.model.id) {
        getCardealership(this.model.id).then(res => {
          this.detailData = res.data
          this.loading = false
        })
      }
      _self.$nextTick(() => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    })
  },
  methods: {
    mapSelect ({ address, lng, lat }) {
      this.detailData.place.address = address
      this.detailData.place.coordinate.longitude = lng
      this.detailData.place.coordinate.latitude = lat
      this.form.setFieldsValue({
        'place.coordinate.longitude': lng,
        'place.coordinate.latitude': lat
      })
    },
    modalOk () {
      this.form.validateFieldsAndScroll((errors, values) => {
        if (!errors) {
          delete values.id
          if (this.model && this.model.id) {
            updateCardealership(this.model.id, values).then(res => {
              this.$emit('ok')
            })
          } else {
            addCardealership(values).then(res => {
              this.$emit('ok')
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
